# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 19:42:22 2020

@author: Farman
"""


from pathlib import Path
import zipfile
import os
import sys



def enum_files_by_ext(path, ext):
    '''
    Enum files by extension.

    Parameters
    ----------
    path : string
        Enum files of.
        
    ext : string
        File extension, with or without dot are both ok.

    Returns
    -------
        File list enumed.
    '''
    p = Path(path)
    
    ext = ext.lstrip('*')
    ext = ext.lstrip('.')
    
    if len(ext) == 0:
        ext = '*'
    
    ext = '*.' + ext    
    
    files = list(p.glob(ext))
    
    base = Path(os.path.abspath(path))
    print(str(base))
    
    files = [str(base / file) for file in files]
    return files



def zip_files(files_to_zip, zip_file_name):
    '''
    Zip files given to a zip file.

    Parameters
    ----------
    file_list_to_zip : string list.
        Files to zip.
        
    zip_file_name : string
        Zip file name to save zipped contants.

    Returns
    -------
        None.

    '''
    with zipfile.ZipFile(zip_file_name, 'w') as myzip:
        for file in files_to_zip:
            print('>>>', file)
            myzip.write(file)
    return



def remove_files(files_to_remove):
    '''
    Remove files given.

    Parameters
    ----------
    files_to_remove : string list.
        file names to remove.

    Returns
    -------
        None.

    '''
    for file in files_to_remove:
        command = '%s "%s"'%('del' if sys.platform == 'win32' else 'rm', file) 
        print('===>', command)
        os.system(command)
    
    return



if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage:')
        print('=' * 40)
        print('python3 ziptxts.py searching_path zip_file_name')
    else:
        files = enum_files_by_ext(sys.argv[1], 'txt')
        print(files)
        zip_files(files, sys.argv[2])
        remove_files(files)